package routes

import (
	"goravel/app/http/controllers"
	"goravel/app/http/middleware"

	"github.com/goravel/framework/contracts/route"
	"github.com/goravel/framework/facades"
)

func Api() {

	userController := controllers.NewUserController()
	categoriesController := controllers.NewCategoriesController()
	permissionController := controllers.NewPermissionsController()
	rolesController := controllers.NewRolesController()

	facades.Route().Prefix("/api/admin").Group(func(router route.Router) {
		router.Post("/users/login", userController.Login)
		router.Post("/users/logout", userController.Logout)
	})

	facades.Route().Prefix("/api/admin").Middleware(middleware.Authenticate(), middleware.CheckPermission()).
		Group(func(router route.Router) {

			// 用户
			router.Resource("/users", userController)
			router.Get("/user/getCurrentUser", userController.GetCurrentUser)
			router.Get("/users/{id}/permissions", userController.GetUserPermissions)
			router.Get("/users/{id}/roles", userController.GetUserRoles)

			// 分类
			router.Resource("/categories", categoriesController)
			router.Get("/categories/subordinate-ids", categoriesController.GetSubordinateIDs)

			// 权限
			router.Get("/permissions", permissionController.Index)
			router.Post("/permissions/assign-to-role", permissionController.AssignPermissionsToRole)
			router.Post("/permissions/assign-roles-to-user", permissionController.AssignRolesToUser)

			// 角色
			router.Resource("/roles", rolesController)
			router.Get("/roles/{id}/permissions", rolesController.GetRolePermissions)
			router.Get("/roles/{id}/users", rolesController.GetRoleUsers)
		})

}
